package y23.Oct;

import java.util.ArrayList;
import java.util.List;

class Desk{
    private int i = 0;
    public synchronized void put(){
        if (i == 1){
            try {
                wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        i++;
        System.out.println(Thread.currentThread().getName()+"往桌子上放了一个包子");
        notifyAll();
    }
    public synchronized void get(){
        if(i == 0){
            try {
                wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        i--;
        System.out.println(Thread.currentThread().getName()+"吃了一个包子");
        notifyAll();
    }
}

public class Test33 {
    public static void main(String[] args) {
        Desk desk = new Desk();
        new Thread(()->{
            while(true){
                desk.put();
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        },"生产者").start();
        new Thread(()->{
            while(true){
                desk.get();
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        },"生产者").start();

    }

}
